package com.cal.utms.model.payment;

import java.util.ArrayList;
import java.util.List;

import com.cal.utms.dao.maintenance.CustomerDaoImpl;
import com.cal.utms.dao.maintenance.IssuerDaoImpl;
import com.cal.utms.dto.payment.AllCustomer;
import com.cal.utms.dto.payment.BankAccountDto;
import com.cal.utms.entity.Customer;
import com.cal.utms.entity.CustomerBankAccount;
import com.cal.utms.entity.Issuer;
import com.cal.utms.entity.IssuerBankAccount;

public class CommenPayementModel {
	CustomerDaoImpl customerDaoImpl = new CustomerDaoImpl();
	IssuerDaoImpl issuerDaoImpl = new IssuerDaoImpl();

	public List<BankAccountDto> getBankAccounts(int customer_id, String customerType) {

		List<BankAccountDto> accountDtos = new ArrayList<BankAccountDto>();

		if (customer_id != 0 && customerType != null) {
			if (customerType.equals("CUS")) {
				List<CustomerBankAccount> bankAccounts = customerDaoImpl.getBankAccountByCustomerId(customer_id);
				for (CustomerBankAccount customerBankAccount : bankAccounts) {
					BankAccountDto accountDto = new BankAccountDto();
					accountDto.setAccountNo(customerBankAccount.getAccountNo());
					accountDto.setBranch(customerBankAccount.getBranch());
					accountDto.setCustomerId(customerBankAccount.getCustomer().getId());
					accountDto.setBankid(customerBankAccount.getId());
					accountDto.setNameOfBank(customerBankAccount.getNameOfBank());

					accountDtos.add(accountDto);
				}
			} else if (customerType.equals("ISU")) {
				List<IssuerBankAccount> issuerBankAccounts = issuerDaoImpl.getBankAccountByIssuer(customer_id);
				for (IssuerBankAccount issuerBankAccount : issuerBankAccounts) {
					BankAccountDto accountDto = new BankAccountDto();
					accountDto.setAccountNo(issuerBankAccount.getAccountNo());
					accountDto.setBranch(issuerBankAccount.getBarnch());
					accountDto.setCustomerId(issuerBankAccount.getIssuer().getId());
					accountDto.setBankid(issuerBankAccount.getId());
					accountDto.setNameOfBank(issuerBankAccount.getNameOfBank());

					accountDtos.add(accountDto);
				}
			} else if (customerType.equals("FEEC")) {
				BankAccountDto accountDto = new BankAccountDto();
//				accountDto.setAccountNo(issuerBankAccount.getAccountNo());
//				accountDto.setBranch(issuerBankAccount.getBarnch());
//				accountDto.setCustomerId(issuerBankAccount.getIssuer().getId());
//				accountDto.setBankid(issuerBankAccount.getId());
//				accountDto.setNameOfBank(issuerBankAccount.getNameOfBank());
				accountDtos.add(accountDto);
			}
		}
		return accountDtos;
	}

	public BankAccountDto getBankAccount(int customer_id, String customerType, int accountid) {

		BankAccountDto accountDto = new BankAccountDto();

		if (customer_id != 0 && customerType != null) {
			if (customerType.equals("CUS")) {
				CustomerBankAccount bankAccounts = customerDaoImpl.getAccountById(accountid);
				if (bankAccounts != null) {
					accountDto.setAccountNo(bankAccounts.getAccountNo());
					accountDto.setBranch(bankAccounts.getBranch());
					accountDto.setCustomerId(bankAccounts.getCustomer().getId());
					accountDto.setBankid(bankAccounts.getId());
					accountDto.setNameOfBank(bankAccounts.getNameOfBank());
				}
			} else if (customerType.equals("ISU")) {
				IssuerBankAccount issuerBankAccount = issuerDaoImpl.getAccountById(accountid);
				if (issuerBankAccount != null) {
					accountDto.setAccountNo(issuerBankAccount.getAccountNo());
					accountDto.setBranch(issuerBankAccount.getBarnch());
					accountDto.setCustomerId(issuerBankAccount.getIssuer().getId());
					accountDto.setBankid(issuerBankAccount.getId());
					accountDto.setNameOfBank(issuerBankAccount.getNameOfBank());
				}
			}
		}
		return accountDto;
	}

	public List<AllCustomer> getCustomerByType(AllCustomer allCustomer) {
		List<AllCustomer> allCustomers = new ArrayList<AllCustomer>();
		if (allCustomer != null) {
			if (allCustomer.getCustomerType().equals("CUS")) {
				List<Customer> customers = customerDaoImpl.getCustomers(allCustomer.getCustomerCode(), allCustomer.getCustomerName());
				for (Customer customer : customers) {
					AllCustomer custo = null;
					if (customer.getAccountType().equals("C")) {
						custo = new AllCustomer(allCustomer.getCustomerType(), customer.getCode(), customer.getSurname(), customer.getId());
					} else {
						custo = new AllCustomer(allCustomer.getCustomerType(), customer.getCode(), customer.getInitials() + " " + customer.getSurname(), customer.getId());
					}

					allCustomers.add(custo);
				}
			} else if (allCustomer.getCustomerType().equals("ISU")) {
				List<Issuer> issuers = issuerDaoImpl.getAllIssuers();
				for (Issuer issuer : issuers) {
					AllCustomer custo = new AllCustomer(allCustomer.getCustomerType(), issuer.getCode(), issuer.getName(), issuer.getId());
					allCustomers.add(custo);
				}
			}
		}
		allCustomer.setSuccess(true);
		return allCustomers;
	}

	public AllCustomer getCustomer(int cusId, String cusType) {
		AllCustomer custo = null;
		if (cusType.equals("CUS")) {
			Customer customer = customerDaoImpl.getCustomerById(cusId);
			if (customer.getAccountType().equals("C")) {
				custo = new AllCustomer(cusType, customer.getCode(), customer.getSurname(), customer.getId());
			} else {
				custo = new AllCustomer(cusType, customer.getCode(), customer.getInitials() + " " + customer.getSurname(), customer.getId());
			}
		} else if (cusType.equals("ISU")) {
			Issuer issuer = issuerDaoImpl.getById(cusId);
			custo = new AllCustomer(cusType, issuer.getCode(), issuer.getName(), issuer.getId());
		}
		return custo;
	}
}
